function pic_replace(dom1, dom2) {
    var file = document.getElementById(dom1).files;
    var reader = new FileReader();
    reader.readAsDataURL(file[0]);
    reader.onload = function (e) {
        var Img = '<img class="act_img_sty" src="' + this.result + '" width="85px" height="100px"/>';
        $('.' + dom2).html(Img)
    }
}

$(document).ready(function () {
    function getData(curr) {

        $.ajax({
            type: "get",
            url: "http://www.puer.zone:30000/ActivityInfo/",
            async: true,
            data: {
                page: curr,
                row: 12
            },
            success: function (data) {
                dataView(data[0]);
                pageV(curr, data[1]);
            }
        });

    }

    getData(1);

    function dataView(data) {
        for (var i = 0; i < data.length; i++) {
            // var delText;
            // if(data[i][2]!=='高级管理员'){
            // 	delText = '<a class="edit_num">删除账号</a>';
            // }else{
            // 	delText = '无权限'
            // }
            var tabsText = `<tr>
      					<td>${data[i][0]}</td>
      					<td>${data[i][1]}</td>
      					<td>${data[i][2]}</td>
      					<td>
      						<img src="${data[i][3]}" height="50"/>
      					</td>
      					<!--<td>${data[i][4]}</td>-->
      					<td index="${data[i][0]}">
      					     <div>
									<a class="active_detail" href="#">查看</a>
									<a class="active_close" href="#">结束</a>
		      		    	 </div>
                        </td>
      		 		</tr>`;
            $('.data_cont').append(tabsText);
        }

        $('.active_detail').click(function () {
            var Index = $(this).parent().parent().attr('index');
            window.location.href = "http://www.puer.zone:9000/active_detail.html?Index=" + Index;
        })

        $('.active_close').click(function () {

            $.ajax({
                type: "get",
                url: "http://www.puer.zone:30000/ActivityEnded",
                async: true,
                data: {
                    activeId: $(this).parent().parent().attr('index')
                },
                success: function (data) {
                    if (data == 'success') {
                        layer.msg('活动结束', {time: 500}, function () {
                            location.reload();
                        })
                    }
                }
            });
        })

    }

    //添加活动
    $('.new_add').click(function () {
        var Index;
        // console.log($.cookie('UserRight'))

        if ($('#active_name').val() == '') {
            layer.msg('活动名不能为空')

        } else if ($('#active_info').val() == '') {
            layer.msg('活动简介不能为空')

        } else if ($('#active_pic').text() == '+') {
            layer.msg('活动图片不能为空')
        } else {
            var formData = new FormData(document.getElementById('active_add'));
            formData.append('ActiveName', $('#active_name').val());
            formData.append('ActiveInfo', $('#active_info').val());

            $.ajax({
                type: "POST",
                url: "http://www.puer.zone:30000/ActivityAdded/",
                async: true,
                data: formData,
                processData: false,
                contentType: false,
                // type:"get",
                // url:"http://www.puer.zone:30000/ActivityAdded",
                // async:true,
                // data:{
                //     UserNo: $('.cl_inp3').val(),
                //     UserName:$('.cl_inp2').val(),
                //     Password:$('.cl_inp4').val(),
                //     Role:Index,
                //     // UserId:$.cookie('UserId')
                // },
                success: function (data) {
                    if (data[0] == 'failure') {
                        if (data[1] == 'name') {
                            console.log(1)
                            layer.msg('活动名重复')
                        } else if (data[1] == 'error') {
                            layer.msg('新增异常请重试')
                        }
                    } else if (data == 'success') {
                        layer.msg('新增成功', {time: 500}, function () {
                            location.reload();
                        });
                    }
                }
            });
            // if($.cookie('UserRight')==4||$.cookie('UserRight')=='10086'){
            //
            // }else{
            // 	layer.msg('无权限');
            // }

        }
    })

    function pageV(curr, page_num) {
        layui.use(['laypage', 'layer'], function () {
            var laypage = layui.laypage
                , layer = layui.layer;
            laypage.render({
                elem: 'demo0'
                , count: page_num
                , limit: 12
                , curr: curr
                , jump: function (obj, first) {
                    if (!first) {
                        $('.data_cont').html('');
                        getData(obj.curr);
                    }
                }
            });
        })
    }

})
